<!doctype html>
<html lang="zh-CN">
<head>
  <base href="https://www.nodeapp.cn/crypto.html" />
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>加密（Crypto） | Node.js 中文文档 | Node.js 中文网</title>
  <meta name="description" content="Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。Node.js 的包管理器 npm，是全球最大的开源库生态系统。">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://www.nodeapp.cn/crypto.html">
  <link rel="apple-touch-icon" href="apple-touch-icon.png">
  <link rel="icon" sizes="32x32" type="image/png" href="favicon.png">
  
  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?acdf78480f7f8f2b23b812565a5868e0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>
<body class="alt apidoc" id="api-section-crypto">
  <div id="content" class="clearfix">
    <div id="column1" data-id="crypto" class="interior">
      <header>
        <h1>Node.js v8.x 中文文档</h1>
        <hr>
      </header>

      <div id="toc">
        <h2>目录</h2>
        <ul>
<li><span class="stability_2"><a href="#crypto_crypto">crypto (加密)</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_determining_if_crypto_support_is_unavailable">Determining if crypto support is unavailable</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_class_certificate">Class: Certificate</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_new_crypto_certificate">new crypto.Certificate()</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_certificate_exportchallenge_spkac">certificate.exportChallenge(spkac)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_certificate_exportpublickey_spkac">certificate.exportPublicKey(spkac)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_certificate_verifyspkac_spkac">certificate.verifySpkac(spkac)</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_cipher">Class: Cipher</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_cipher_final_outputencoding">cipher.final([outputEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_cipher_setaad_buffer">cipher.setAAD(buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_cipher_getauthtag">cipher.getAuthTag()</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_cipher_setautopadding_autopadding">cipher.setAutoPadding([autoPadding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_cipher_update_data_inputencoding_outputencoding">cipher.update(data[, inputEncoding][, outputEncoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_decipher">Class: Decipher</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_decipher_final_outputencoding">decipher.final([outputEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_decipher_setaad_buffer">decipher.setAAD(buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_decipher_setauthtag_buffer">decipher.setAuthTag(buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_decipher_setautopadding_autopadding">decipher.setAutoPadding([autoPadding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_decipher_update_data_inputencoding_outputencoding">decipher.update(data[, inputEncoding][, outputEncoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_diffiehellman">Class: DiffieHellman</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding">diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_generatekeys_encoding">diffieHellman.generateKeys([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_getgenerator_encoding">diffieHellman.getGenerator([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_getprime_encoding">diffieHellman.getPrime([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_getprivatekey_encoding">diffieHellman.getPrivateKey([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_getpublickey_encoding">diffieHellman.getPublicKey([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_setprivatekey_privatekey_encoding">diffieHellman.setPrivateKey(privateKey[, encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_setpublickey_publickey_encoding">diffieHellman.setPublicKey(publicKey[, encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_diffiehellman_verifyerror">diffieHellman.verifyError</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_ecdh">Class: ECDH</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding">ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_ecdh_generatekeys_encoding_format">ecdh.generateKeys([encoding[, format]])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_ecdh_getprivatekey_encoding">ecdh.getPrivateKey([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_ecdh_getpublickey_encoding_format">ecdh.getPublicKey([encoding][, format])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_ecdh_setprivatekey_privatekey_encoding">ecdh.setPrivateKey(privateKey[, encoding])</a></span></li>
<li><span class="stability_0"><a href="#crypto_ecdh_setpublickey_publickey_encoding">ecdh.setPublicKey(publicKey[, encoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_hash">Class: Hash</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_hash_digest_encoding">hash.digest([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_hash_update_data_inputencoding">hash.update(data[, inputEncoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_hmac">Class: Hmac</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_hmac_digest_encoding">hmac.digest([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_hmac_update_data_inputencoding">hmac.update(data[, inputEncoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_sign">Class: Sign</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_sign_sign_privatekey_outputformat">sign.sign(privateKey[, outputFormat])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_sign_update_data_inputencoding">sign.update(data[, inputEncoding])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_class_verify">Class: Verify</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_verify_update_data_inputencoding">verify.update(data[, inputEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_verify_verify_object_signature_signatureformat">verify.verify(object, signature[, signatureFormat])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_crypto_module_methods_and_properties"><code>crypto</code> module methods and properties</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_crypto_constants">crypto.constants</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_default_encoding">crypto.DEFAULT_ENCODING</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_fips">crypto.fips</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createcipher_algorithm_password_options">crypto.createCipher(algorithm, password[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options">crypto.createCipheriv(algorithm, key, iv[, options])</a></span></li>
<li><span class="stability_0"><a href="#crypto_crypto_createcredentials_details">crypto.createCredentials(details)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createdecipher_algorithm_password_options">crypto.createDecipher(algorithm, password[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options">crypto.createDecipheriv(algorithm, key, iv[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding">crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_creatediffiehellman_primelength_generator">crypto.createDiffieHellman(primeLength[, generator])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createecdh_curvename">crypto.createECDH(curveName)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createhash_algorithm_options">crypto.createHash(algorithm[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createhmac_algorithm_key_options">crypto.createHmac(algorithm, key[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createsign_algorithm_options">crypto.createSign(algorithm[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_createverify_algorithm_options">crypto.createVerify(algorithm[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_getciphers">crypto.getCiphers()</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_getcurves">crypto.getCurves()</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_getdiffiehellman_groupname">crypto.getDiffieHellman(groupName)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_gethashes">crypto.getHashes()</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback">crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest">crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_privatedecrypt_privatekey_buffer">crypto.privateDecrypt(privateKey, buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_privateencrypt_privatekey_buffer">crypto.privateEncrypt(privateKey, buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_publicdecrypt_key_buffer">crypto.publicDecrypt(key, buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_publicencrypt_key_buffer">crypto.publicEncrypt(key, buffer)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_randombytes_size_callback">crypto.randomBytes(size[, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_randomfillsync_buffer_offset_size">crypto.randomFillSync(buffer[, offset][, size])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_randomfill_buffer_offset_size_callback">crypto.randomFill(buffer[, offset][, size], callback)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_setengine_engine_flags">crypto.setEngine(engine[, flags])</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_crypto_timingsafeequal_a_b">crypto.timingSafeEqual(a, b)</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_notes">Notes</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_legacy_streams_api_pre_node_js_v0_10">Legacy Streams API (pre Node.js v0.10)</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_recent_ecdh_changes">Recent ECDH Changes</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_support_for_weak_or_compromised_algorithms">Support for weak or compromised algorithms</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#crypto_crypto_constants_1">Crypto Constants</a></span><ul>
<li><span class="stability_undefined"><a href="#crypto_openssl_options">OpenSSL Options</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_openssl_engine_constants">OpenSSL Engine Constants</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_other_openssl_constants">Other OpenSSL Constants</a></span></li>
<li><span class="stability_undefined"><a href="#crypto_node_js_crypto_constants">Node.js Crypto Constants</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>
<div id="apicontent">
        <h1>crypto (加密)<span><a class="mark" href="#crypto_crypto" id="crypto_crypto">#</a></span></h1>
<!--introduced_in=v0.3.6-->
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">稳定性: 2</a> - 稳定的</div><p><code>crypto</code> 模块提供了加密功能，包含对 OpenSSL 的哈希、HMAC、加密、解密、签名、以及验证功能的一整套封装。</p>
<p>使用 <code>require(&apos;crypto&apos;)</code> 来访问该模块。</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);

const secret = &apos;abcdefg&apos;;
const hash = crypto.createHmac(&apos;sha256&apos;, secret)
                   .update(&apos;I love cupcakes&apos;)
                   .digest(&apos;hex&apos;);
console.log(hash);
// Prints:
//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
</code></pre>
<h2>Determining if crypto support is unavailable<span><a class="mark" href="#crypto_determining_if_crypto_support_is_unavailable" id="crypto_determining_if_crypto_support_is_unavailable">#</a></span></h2>
<p>可以在不包括支持 <code>crypto</code> 模块的情况下构建 Node.js, 这时, 调用 <code>require(&apos;crypto&apos;)</code> 将
导致抛出异常.</p>
<pre><code class="lang-js">let crypto;
try {
  crypto = require(&apos;crypto&apos;);
} catch (err) {
  console.log(&apos;不支持 crypto!&apos;);
}
</code></pre>
<h2>Class: Certificate<span><a class="mark" href="#crypto_class_certificate" id="crypto_class_certificate">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.11.8</span>
</div><p>SPKAC 最初是由 Netscape 实现的一种证书签名请求机制, 现在正式成为 <a href="http://www.w3.org/TR/html5/forms.html#the-keygen-element">HTML5&apos;s <code>keygen</code> element</a> 的一部分.</p>
<p><code>crypto</code> 模块提供 <code>Certificate</code> 类用于处理 SPKAC 数据. 最普遍的用法是处理 HTML5 <code>keygen</code> 元素
产生的输出. Node.js 内部使用 <a href="https://www.openssl.org/docs/man1.0.2/apps/spkac.html">OpenSSL&apos;s SPKAC implementation</a> 处理.</p>
<h3>new crypto.Certificate()<span><a class="mark" href="#crypto_new_crypto_certificate" id="crypto_new_crypto_certificate">#</a></span></h3>
<p>可以使用 <code>new</code> 关键字或者调用 <code>crypto.Certificate()</code> 方法创建 <code>Certificate</code> 类的实例:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);

const cert1 = new crypto.Certificate();
const cert2 = crypto.Certificate();
</code></pre>
<h3>certificate.exportChallenge(spkac)<span><a class="mark" href="#crypto_certificate_exportchallenge_spkac" id="crypto_certificate_exportchallenge_spkac">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.8</span>
</div><ul>
<li><code>spkac</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>返回 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> 返回 <code>spkac</code> 数据结构的 challenge 部分，<code>spkac</code> 包含一个公钥和一个 challange。</li>
</ul>
<pre><code class="lang-js">const cert = require(&apos;crypto&apos;).Certificate();
const spkac = getSpkacSomehow();
const challenge = cert.exportChallenge(spkac);
console.log(challenge.toString(&apos;utf8&apos;));
// Prints: the challenge as a UTF8 string
</code></pre>
<h3>certificate.exportPublicKey(spkac)<span><a class="mark" href="#crypto_certificate_exportpublickey_spkac" id="crypto_certificate_exportpublickey_spkac">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.8</span>
</div><ul>
<li><code>spkac</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>返回 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> 数据结构的公钥部分，<code>spkac</code> 包含一个公钥和一个 challange。</li>
</ul>
<pre><code class="lang-js">const cert = require(&apos;crypto&apos;).Certificate();
const spkac = getSpkacSomehow();
const publicKey = cert.exportPublicKey(spkac);
console.log(publicKey);
// Prints: the public key as &lt;Buffer ...&gt;
</code></pre>
<h3>certificate.verifySpkac(spkac)<span><a class="mark" href="#crypto_certificate_verifyspkac_spkac" id="crypto_certificate_verifyspkac_spkac">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.8</span>
</div><ul>
<li><code>spkac</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>返回 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> 如果 <code>spkac</code> 数据结构是有效的返回 <code>true</code>，否则返回 <code>false</code>。</li>
</ul>
<pre><code class="lang-js">const cert = require(&apos;crypto&apos;).Certificate();
const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(Buffer.from(spkac)));
// Prints: true 或者 false
</code></pre>
<h2>Class: Cipher<span><a class="mark" href="#crypto_class_cipher" id="crypto_class_cipher">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><p><code>Cipher</code>类的实例用于加密数据。这个类可以用在以下两种方法中的一种:</p>
<ul>
<li>作为<a href="stream.html">stream</a>，既可读又可写，未加密数据的编写是为了在可读的方面生成加密的数据，或者</li>
<li>使用<a href="#crypto_cipher_update_data_input_encoding_output_encoding"><code>cipher.update()</code></a>和<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>方法产生加密的数据。</li>
</ul>
<p><a href="#crypto_crypto_createcipher_algorithm_password"><code>crypto.createCipher()</code></a>或<a href="#crypto_crypto_createcipheriv_algorithm_key_iv"><code>crypto.createCipheriv()</code></a>方法用于创建<code>Cipher</code>实例。<code>Cipher</code>对象不能直接使用<code>new</code>关键字创建。</p>
<p>示例:使用<code>Cipher</code>对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const cipher = crypto.createCipher(&apos;aes192&apos;, &apos;a password&apos;);

let encrypted = &apos;&apos;;
cipher.on(&apos;readable&apos;, () =&gt; {
  const data = cipher.read();
  if (data)
    encrypted += data.toString(&apos;hex&apos;);
});
cipher.on(&apos;end&apos;, () =&gt; {
  console.log(encrypted);
  // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
});

cipher.write(&apos;some clear text data&apos;);
cipher.end();
</code></pre>
<p>示例:使用<code>Cipher</code>和管道流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);
const cipher = crypto.createCipher(&apos;aes192&apos;, &apos;a password&apos;);

const input = fs.createReadStream(&apos;test.js&apos;);
const output = fs.createWriteStream(&apos;test.enc&apos;);

input.pipe(cipher).pipe(output);
</code></pre>
<p>示例:使用<a href="#crypto_cipher_update_data_input_encoding_output_encoding"><code>cipher.update()</code></a>和<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>方法:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const cipher = crypto.createCipher(&apos;aes192&apos;, &apos;a password&apos;);

let encrypted = cipher.update(&apos;some clear text data&apos;, &apos;utf8&apos;, &apos;hex&apos;);
encrypted += cipher.final(&apos;hex&apos;);
console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
</code></pre>
<h3>cipher.final([outputEncoding])<span><a class="mark" href="#crypto_cipher_final_outputencoding" id="crypto_cipher_final_outputencoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>返回任何加密的内容。如果 <code>outputEncoding</code> 参数是<code>&apos;latin1&apos;</code>, <code>&apos;base64&apos;</code> 或者 <code>&apos;hex&apos;</code>，返回字符串。
如果没有提供 <code>outputEncoding</code>，则返回<a href="buffer.html"><code>Buffer</code></a>。</p>
<p>一旦<code>cipher.final()</code>方法已被调用，<code>Cipher</code> 对象就不能再用于加密数据。如果试图再次调用<code>cipher.final()</code>，将会抛出一个错误。</p>
<h3>cipher.setAAD(buffer)<span><a class="mark" href="#crypto_cipher_setaad_buffer" id="crypto_cipher_setaad_buffer">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v1.0.0</span>
</div><ul>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a></li>
<li>返回<span class="type">&lt;Cipher&gt;</span>方法链。</li>
</ul>
<p>当使用经过验证的加密模式(目前只支持<code>GCM</code>)时，<code>cipher.setAAD()</code>方法设置用于<em>additional authenticated data</em>(<em>附加验证的data</em>(AAD))输入参数的值。</p>
<p><code>cipher.setAAD()</code>法必须在<a href="#crypto_cipher_update_data_input_encoding_output_encoding"><code>cipher.update()</code></a>之前调用。</p>
<h3>cipher.getAuthTag()<span><a class="mark" href="#crypto_cipher_getauthtag" id="crypto_cipher_getauthtag">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v1.0.0</span>
</div><p>当使用经验证的加密模式时(目前只有<code>GCM</code>支持),<code>cipher.getAuthTag()</code>方法返回一个<a href="buffer.html"><code>Buffer</code></a>，此<a href="buffer.html"><code>Buffer</code></a>包含已从给定数据计算后的<em>authentication tag</em>。
<code>cipher.getAuthTag()</code>方法只能在使用<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>方法完全加密后调用。</p>
<h3>cipher.setAutoPadding([autoPadding])<span><a class="mark" href="#crypto_cipher_setautopadding_autopadding" id="crypto_cipher_setautopadding_autopadding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.7.1</span>
</div><ul>
<li><code>autoPadding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> 默认为 <code>true</code>.</li>
<li>返回<span class="type">&lt;Cipher&gt;</span>方法链。</li>
</ul>
<p>当使用块加密算法时，<code>Cipher</code>类会自动添加padding到输入数据中，来适配相应块大小。可调用<code>cipher.setAutoPadding(false)</code>禁用默认padding。</p>
<p>当<code>autoPadding</code>是<code>false</code>时，整个输入数据的长度必须是cipher块大小的倍数，否则<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>将抛出一个错误。
禁用自动填充对于非标准填充是有用的，例如使用<code>0x0</code>代替PKCS填充。</p>
<p><code>cipher.setAutoPadding()</code>必须在<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>之前被调用。</p>
<h3>cipher.update(data[, inputEncoding][, outputEncoding])<span><a class="mark" href="#crypto_cipher_update_data_inputencoding_outputencoding" id="crypto_cipher_update_data_inputencoding_outputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>默认<code>inputEncoding</code>由<code>binary</code> 变为 <code>utf8</code></p>
</td></tr>
<tr><td>v0.1.94</td>
<td><p><span>新增于: v0.1.94</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>用<code>data</code>更新密码。如果给出了<code>inputEncoding</code>的论证，它的值必须是<code>&apos;utf8&apos;</code>, <code>&apos;ascii&apos;</code>, 或者<code>&apos;latin1&apos;</code>，而<code>data</code>参数是使用指定编码的字符串。如果不给出<code>inputEncoding</code>的参数，则<code>data</code>必须是<a href="buffer.html"><code>Buffer</code></a>，<code>TypedArray</code>， 或者<code>DataView</code>。如果<code>data</code>是一个<a href="buffer.html"><code>Buffer</code></a>，<code>TypedArray</code>， 或者 <code>DataView</code>， 那么<code>inputEncoding</code>就被忽略了。</p>
<p><code>outputEncoding</code>指定了加密数据的输出格式，可以是<code>&apos;latin1&apos;</code>， <code>&apos;base64&apos;</code> 或者 <code>&apos;hex&apos;</code>。如果指定了<code>outputEncoding</code>，则返回使用指定编码的字符串。如果没有<code>outputEncoding</code>被提供，会返回<a href="buffer.html"><code>Buffer</code></a>。</p>
<p><code>cipher.update()</code>方法可以用新数据多次调用，直到<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>被调用。
[&apos; cipher.final()&apos;][]。在<a href="#crypto_cipher_final_output_encoding"><code>cipher.final()</code></a>之后调用<code>cipher.update()</code>将抛出错误。</p>
<h2>Class: Decipher<span><a class="mark" href="#crypto_class_decipher" id="crypto_class_decipher">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><p><code>Decipher</code>类的实例用于解密数据。这个类可以用在以下两种方法中的一种:</p>
<ul>
<li>作为<a href="stream.html">stream</a>，既可读，又可写，加密数据的编写是为了在可读的方面生成未加密的数据</li>
<li>使用<a href="#crypto_decipher_update_data_input_encoding_output_encoding"><code>decipher.update()</code></a>和<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>方法产生未加密的数据。</li>
</ul>
<p><a href="#crypto_crypto_createdecipher_algorithm_password"><code>crypto.createDecipher()</code></a>或<a href="#crypto_crypto_createdecipheriv_algorithm_key_iv"><code>crypto.createDecipheriv()</code></a>的方法
用于创建<code>Decipher</code>实例。<code>Decipher</code>对象不能直接使用<code>new</code>关键字创建。</p>
<p>示例:使用<code>Decipher</code>对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const decipher = crypto.createDecipher(&apos;aes192&apos;, &apos;a password&apos;);

let decrypted = &apos;&apos;;
decipher.on(&apos;readable&apos;, () =&gt; {
  const data = decipher.read();
  if (data)
    decrypted += data.toString(&apos;utf8&apos;);
});
decipher.on(&apos;end&apos;, () =&gt; {
  console.log(decrypted);
  // Prints: some clear text data
});

const encrypted =
    &apos;ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504&apos;;
decipher.write(encrypted, &apos;hex&apos;);
decipher.end();
</code></pre>
<p>示例:使用<code>Decipher</code>和管道流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);
const decipher = crypto.createDecipher(&apos;aes192&apos;, &apos;a password&apos;);

const input = fs.createReadStream(&apos;test.enc&apos;);
const output = fs.createWriteStream(&apos;test.js&apos;);

input.pipe(decipher).pipe(output);
</code></pre>
<p>示例:使用<a href="#crypto_decipher_update_data_input_encoding_output_encoding"><code>decipher.update()</code></a>和<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>方法:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const decipher = crypto.createDecipher(&apos;aes192&apos;, &apos;a password&apos;);

const encrypted =
    &apos;ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504&apos;;
let decrypted = decipher.update(encrypted, &apos;hex&apos;, &apos;utf8&apos;);
decrypted += decipher.final(&apos;utf8&apos;);
console.log(decrypted);
// Prints: some clear text data
</code></pre>
<h3>decipher.final([outputEncoding])<span><a class="mark" href="#crypto_decipher_final_outputencoding" id="crypto_decipher_final_outputencoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>返回任何剩余的解密内容。如果<code>outputEncoding</code>参数是&apos;latin1&apos;，&apos;ascii&apos;或&apos;utf8&apos;之一，则返回一个字符串。
如果未提供输出编码，则返回<code>Buffer</code>。</p>
<p>一旦调用了<code>decipher.final()</code>方法，<code>Decipher</code>对象就不能再用于解密数据。
试图不止一次调用<code>decipher.final()</code>会导致错误被抛出。</p>
<h3>decipher.setAAD(buffer)<span><a class="mark" href="#crypto_decipher_setaad_buffer" id="crypto_decipher_setaad_buffer">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.2.0</td>
<td><p>This method now returns a reference to <code>decipher</code>.</p>
</td></tr>
<tr><td>v1.0.0</td>
<td><p><span>新增于: v1.0.0</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>返回<span class="type">&lt;cipher&gt;</span>的一个方法链。</li>
</ul>
<p>当使用经过验证的加密模式（当前仅支持<code>GCM</code>）时，<code>decipher.setAAD()</code>方法会设置用于附加验证数据(AAD)输入参数的值。</p>
<p><code>decipher.setAAD()</code> 必须在<a href="#crypto_decipher_update_data_input_encoding_output_encoding"><code>decipher.update()</code></a>之前被调用。</p>
<h3>decipher.setAuthTag(buffer)<span><a class="mark" href="#crypto_decipher_setauthtag_buffer" id="crypto_decipher_setauthtag_buffer">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.2.0</td>
<td><p>This method now returns a reference to <code>decipher</code>.</p>
</td></tr>
<tr><td>v1.0.0</td>
<td><p><span>新增于: v1.0.0</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>Returns the <span class="type">&lt;Cipher&gt;</span> for method chaining.</li>
</ul>
<p>When using an authenticated encryption mode (only <code>GCM</code> is currently
supported), the <code>decipher.setAuthTag()</code> method is used to pass in the
received <em>authentication tag</em>. If no tag is provided, or if the cipher text
has been tampered with, <a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a> will throw, indicating that the
cipher text should be discarded due to failed authentication.</p>
<p>The <code>decipher.setAuthTag()</code> method must be called before
<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>.</p>
<h3>decipher.setAutoPadding([autoPadding])<span><a class="mark" href="#crypto_decipher_setautopadding_autopadding" id="crypto_decipher_setautopadding_autopadding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.7.1</span>
</div><ul>
<li><code>autoPadding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> Defaults to <code>true</code>.</li>
<li>Returns the <span class="type">&lt;Cipher&gt;</span> for method chaining.</li>
</ul>
<p>When data has been encrypted without standard block padding, calling
<code>decipher.setAutoPadding(false)</code> will disable automatic padding to prevent
<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a> from checking for and removing padding.</p>
<p>Turning auto padding off will only work if the input data&apos;s length is a
multiple of the ciphers block size.</p>
<p>The <code>decipher.setAutoPadding()</code> method must be called before
<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>.</p>
<h3>decipher.update(data[, inputEncoding][, outputEncoding])<span><a class="mark" href="#crypto_decipher_update_data_inputencoding_outputencoding" id="crypto_decipher_update_data_inputencoding_outputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.1.94</td>
<td><p><span>新增于: v0.1.94</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>使用新数据更新解密。如果给出<code>inputEncoding</code>参数，它的值必须是<code>&apos;latin1&apos;</code>, <code>&apos;base64&apos;</code>或<code>&apos;hex&apos;</code>中的一个，<code>data</code>参数是使用指定编码的字符串。如果未给出<code>inputEncoding</code>参数，则<code>data</code>必须是<a href="buffer.html"><code>Buffer</code></a>。如果<code>data</code>是<a href="buffer.html"><code>Buffer</code></a>，则忽略<code>inputEncoding</code>。</p>
<p><code>outputEncoding</code>指定加密数据的输出格式，可以是<code>&apos;latin1&apos;</code>, <code>&apos;ascii&apos;</code>或<code>&apos;utf8&apos;</code>。如果指定了<code>outputEncoding</code>，则返回使用指定编码的字符串。如果未提供<code>outputEncoding</code>，则返回<a href="buffer.html"><code>Buffer</code></a>。</p>
<p>可以使用新数据多次调用<code>decipher.update()</code>方法，直到调用<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>。在<a href="#crypto_decipher_final_output_encoding"><code>decipher.final()</code></a>之后调用<code>decipher.update()</code>会导致抛出错误。</p>
<h2>Class: DiffieHellman<span><a class="mark" href="#crypto_class_diffiehellman" id="crypto_class_diffiehellman">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><p><code>DiffieHellman</code>类是一个用来创建Diffie-Hellman键交换的工具。
<code>DiffieHellman</code>类的实例可以使用<a href="#crypto_crypto_creatediffiehellman_prime_prime_encoding_generator_generator_encoding"><code>crypto.createDiffieHellman()</code></a>方法。</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const assert = require(&apos;assert&apos;);

// Generate Alice&apos;s keys...
const alice = crypto.createDiffieHellman(2048);
const aliceKey = alice.generateKeys();

// Generate Bob&apos;s keys...
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys();

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

// OK
assert.strictEqual(aliceSecret.toString(&apos;hex&apos;), bobSecret.toString(&apos;hex&apos;));
</code></pre>
<h3>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])<span><a class="mark" href="#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding" id="crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>otherPublicKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Computes the shared secret using <code>otherPublicKey</code> as the other
party&apos;s public key and returns the computed shared secret. The supplied
key is interpreted using the specified <code>inputEncoding</code>, and secret is
encoded using specified <code>outputEncoding</code>. Encodings can be
<code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If the <code>inputEncoding</code> is not
provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
<code>TypedArray</code>, or <code>DataView</code>.</p>
<p>If <code>outputEncoding</code> is given a string is returned; otherwise, a
<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.generateKeys([encoding])<span><a class="mark" href="#crypto_diffiehellman_generatekeys_encoding" id="crypto_diffiehellman_generatekeys_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Generates private and public Diffie-Hellman key values, and returns
the public key in the specified <code>encoding</code>. This key should be
transferred to the other party. Encoding can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>,
or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided a string is returned; otherwise a
<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.getGenerator([encoding])<span><a class="mark" href="#crypto_diffiehellman_getgenerator_encoding" id="crypto_diffiehellman_getgenerator_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>, which can
be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If  <code>encoding</code> is provided a string is
returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.getPrime([encoding])<span><a class="mark" href="#crypto_diffiehellman_getprime_encoding" id="crypto_diffiehellman_getprime_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>, which can
be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided a string is
returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.getPrivateKey([encoding])<span><a class="mark" href="#crypto_diffiehellman_getprivatekey_encoding" id="crypto_diffiehellman_getprivatekey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>,
which can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided a
string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.getPublicKey([encoding])<span><a class="mark" href="#crypto_diffiehellman_getpublickey_encoding" id="crypto_diffiehellman_getpublickey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>, which
can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided a
string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>diffieHellman.setPrivateKey(privateKey[, encoding])<span><a class="mark" href="#crypto_diffiehellman_setprivatekey_privatekey_encoding" id="crypto_diffiehellman_setprivatekey_privatekey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>privateKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided
and is either <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>, <code>privateKey</code> is expected
to be a string. If no <code>encoding</code> is provided, <code>privateKey</code> is expected
to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p>
<h3>diffieHellman.setPublicKey(publicKey[, encoding])<span><a class="mark" href="#crypto_diffiehellman_setpublickey_publickey_encoding" id="crypto_diffiehellman_setpublickey_publickey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>publicKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided
and is either <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code> or <code>&apos;base64&apos;</code>, <code>publicKey</code> is expected
to be a string. If no <code>encoding</code> is provided, <code>publicKey</code> is expected
to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p>
<h3>diffieHellman.verifyError<span><a class="mark" href="#crypto_diffiehellman_verifyerror" id="crypto_diffiehellman_verifyerror">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.12</span>
</div><p>A bit field containing any warnings and/or errors resulting from a check
performed during initialization of the <code>DiffieHellman</code> object.</p>
<p>The following values are valid for this property (as defined in <code>constants</code>
module):</p>
<ul>
<li><code>DH_CHECK_P_NOT_SAFE_PRIME</code></li>
<li><code>DH_CHECK_P_NOT_PRIME</code></li>
<li><code>DH_UNABLE_TO_CHECK_GENERATOR</code></li>
<li><code>DH_NOT_SUITABLE_GENERATOR</code></li>
</ul>
<h2>Class: ECDH<span><a class="mark" href="#crypto_class_ecdh" id="crypto_class_ecdh">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><p><code>ECDH</code>类是创建椭圆曲线Diffie-Hellman（Elliptic Curve Diffie-Hellman (ECDH)）键交换的实用工具。
<code>ECDH</code>类的实例可以使用<a href="#crypto_crypto_createecdh_curve_name"><code>crypto.createECDH()</code></a>方法。</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const assert = require(&apos;assert&apos;);

// Generate Alice&apos;s keys...
const alice = crypto.createECDH(&apos;secp521r1&apos;);
const aliceKey = alice.generateKeys();

// Generate Bob&apos;s keys...
const bob = crypto.createECDH(&apos;secp521r1&apos;);
const bobKey = bob.generateKeys();

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

assert.strictEqual(aliceSecret.toString(&apos;hex&apos;), bobSecret.toString(&apos;hex&apos;));
// OK
</code></pre>
<h3>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])<span><a class="mark" href="#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding" id="crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.11.14</td>
<td><p><span>新增于: v0.11.14</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>otherPublicKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>outputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Computes the shared secret using <code>otherPublicKey</code> as the other
party&apos;s public key and returns the computed shared secret. The supplied
key is interpreted using specified <code>inputEncoding</code>, and the returned secret
is encoded using the specified <code>outputEncoding</code>. Encodings can be
<code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If the <code>inputEncoding</code> is not
provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
<code>DataView</code>.</p>
<p>If <code>outputEncoding</code> is given a string will be returned; otherwise a
<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>ecdh.generateKeys([encoding[, format]])<span><a class="mark" href="#crypto_ecdh_generatekeys_encoding_format" id="crypto_ecdh_generatekeys_encoding_format">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>format</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Defaults to <code>uncompressed</code>.</li>
</ul>
<p>Generates private and public EC Diffie-Hellman key values, and returns
the public key in the specified <code>format</code> and <code>encoding</code>. This key should be
transferred to the other party.</p>
<p>The <code>format</code> argument specifies point encoding and can be <code>&apos;compressed&apos;</code> or
<code>&apos;uncompressed&apos;</code>. If <code>format</code> is not specified, the point will be returned in
<code>&apos;uncompressed&apos;</code> format.</p>
<p>The <code>encoding</code> argument can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If
<code>encoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a>
is returned.</p>
<h3>ecdh.getPrivateKey([encoding])<span><a class="mark" href="#crypto_ecdh_getprivatekey_encoding" id="crypto_ecdh_getprivatekey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Returns the EC Diffie-Hellman private key in the specified <code>encoding</code>,
which can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided
a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<h3>ecdh.getPublicKey([encoding][, format])<span><a class="mark" href="#crypto_ecdh_getpublickey_encoding_format" id="crypto_ecdh_getpublickey_encoding_format">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>format</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Defaults to <code>uncompressed</code>.</li>
</ul>
<p>Returns the EC Diffie-Hellman public key in the specified <code>encoding</code> and
<code>format</code>.</p>
<p>The <code>format</code> argument specifies point encoding and can be <code>&apos;compressed&apos;</code> or
<code>&apos;uncompressed&apos;</code>. If <code>format</code> is not specified the point will be returned in
<code>&apos;uncompressed&apos;</code> format.</p>
<p>The <code>encoding</code> argument can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>. If
<code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is
returned.</p>
<h3>ecdh.setPrivateKey(privateKey[, encoding])<span><a class="mark" href="#crypto_ecdh_setprivatekey_privatekey_encoding" id="crypto_ecdh_setprivatekey_privatekey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>privateKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Sets the EC Diffie-Hellman private key. The <code>encoding</code> can be <code>&apos;latin1&apos;</code>,
<code>&apos;hex&apos;</code> or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided, <code>privateKey</code> is expected
to be a string; otherwise <code>privateKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
<code>TypedArray</code>, or <code>DataView</code>.</p>
<p>If <code>privateKey</code> is not valid for the curve specified when the <code>ECDH</code> object was
created, an error is thrown. Upon setting the private key, the associated
public point (key) is also generated and set in the ECDH object.</p>
<h3>ecdh.setPublicKey(publicKey[, encoding])<span><a class="mark" href="#crypto_ecdh_setpublickey_publickey_encoding" id="crypto_ecdh_setpublickey_publickey_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span><span>废弃于: v5.2.0</span>
</div><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><ul>
<li><code>publicKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Sets the EC Diffie-Hellman public key. Key encoding can be <code>&apos;latin1&apos;</code>,
<code>&apos;hex&apos;</code> or <code>&apos;base64&apos;</code>. If <code>encoding</code> is provided <code>publicKey</code> is expected to
be a string; otherwise a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
<p>Note that there is not normally a reason to call this method because <code>ECDH</code>
only requires a private key and the other party&apos;s public key to compute the
shared secret. Typically either <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a> or
<a href="#crypto_ecdh_setprivatekey_private_key_encoding"><code>ecdh.setPrivateKey()</code></a> will be called. The <a href="#crypto_ecdh_setprivatekey_private_key_encoding"><code>ecdh.setPrivateKey()</code></a> method
attempts to generate the public point/key associated with the private key being
set.</p>
<p>Example (obtaining a shared secret):</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const alice = crypto.createECDH(&apos;secp256k1&apos;);
const bob = crypto.createECDH(&apos;secp256k1&apos;);

// Note: This is a shortcut way to specify one of Alice&apos;s previous private
// keys. It would be unwise to use such a predictable private key in a real
// application.
alice.setPrivateKey(
  crypto.createHash(&apos;sha256&apos;).update(&apos;alice&apos;, &apos;utf8&apos;).digest()
);

// Bob uses a newly generated cryptographically strong
// pseudorandom key pair
bob.generateKeys();

const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, &apos;hex&apos;);
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, &apos;hex&apos;);

// aliceSecret and bobSecret should be the same shared secret value
console.log(aliceSecret === bobSecret);
</code></pre>
<h2>Class: Hash<span><a class="mark" href="#crypto_class_hash" id="crypto_class_hash">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><p><code>Hash</code>类是用于创建数据哈希值的工具类。它能用以下方法使用：</p>
<ul>
<li><p>作为一个<a href="stream.html">stream</a>，它既可读又可写，数据被写入要在可读的方面生成一个计算散列摘要</p>
</li>
<li><p>使用<a href="#crypto_hash_update_data_input_encoding"><code>hash.update()</code></a>和<a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a>方法产生计算后的哈希。</p>
</li>
</ul>
<p><a href="#crypto_crypto_createhash_algorithm"><code>crypto.createHash()</code></a>方法用于创建<code>Hash</code>实例。<code>Hash</code>不能直接使用<code>new</code>关键字创建对象。</p>
<p>示例: 使用<code>Hash</code>对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const hash = crypto.createHash(&apos;sha256&apos;);

hash.on(&apos;readable&apos;, () =&gt; {
  const data = hash.read();
  if (data) {
    console.log(data.toString(&apos;hex&apos;));
    // Prints:
    //   6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
  }
});

hash.write(&apos;some data to hash&apos;);
hash.end();
</code></pre>
<p>示例:使用 <code>Hash</code> 和管道流</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);
const hash = crypto.createHash(&apos;sha256&apos;);

const input = fs.createReadStream(&apos;test.js&apos;);
input.pipe(hash).pipe(process.stdout);
</code></pre>
<p>示例:使用<a href="#crypto_hash_update_data_input_encoding"><code>hash.update()</code></a>和<a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a></p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const hash = crypto.createHash(&apos;sha256&apos;);

hash.update(&apos;some data to hash&apos;);
console.log(hash.digest(&apos;hex&apos;));
// Prints:
//   6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
</code></pre>
<h3>hash.digest([encoding])<span><a class="mark" href="#crypto_hash_digest_encoding" id="crypto_hash_digest_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Calculates the digest of all of the data passed to be hashed (using the
<a href="#crypto_hash_update_data_input_encoding"><code>hash.update()</code></a> method). The <code>encoding</code> can be <code>&apos;hex&apos;</code>, <code>&apos;latin1&apos;</code> or
<code>&apos;base64&apos;</code>. If <code>encoding</code> is provided a string will be returned; otherwise
a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
<p>The <code>Hash</code> object can not be used again after <code>hash.digest()</code> method has been
called. Multiple calls will cause an error to be thrown.</p>
<h3>hash.update(data[, inputEncoding])<span><a class="mark" href="#crypto_hash_update_data_inputencoding" id="crypto_hash_update_data_inputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.1.92</td>
<td><p><span>新增于: v0.1.92</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Updates the hash content with the given <code>data</code>, the encoding of which
is given in <code>inputEncoding</code> and can be <code>&apos;utf8&apos;</code>, <code>&apos;ascii&apos;</code> or
<code>&apos;latin1&apos;</code>. If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
encoding of <code>&apos;utf8&apos;</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
<p>This can be called many times with new data as it is streamed.</p>
<h2>Class: Hmac<span><a class="mark" href="#crypto_class_hmac" id="crypto_class_hmac">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><p><code>Hmac</code>类是用于创建加密Hmac摘要的工具。它可以有两种用法:</p>
<ul>
<li>作为<a href="stream.html">stream</a>,它既可读又可写，数据被写入要在可读的方面生成一个经过计算的HMAC摘要。</li>
<li>使用<a href="#crypto_hmac_update_data_input_encoding"><code>hmac.update()</code></a>和<a href="#crypto_hmac_digest_encoding"><code>hmac.digest()</code></a>方法产生计算后的HMAC摘要。</li>
</ul>
<p><a href="#crypto_crypto_createhmac_algorithm_key"><code>crypto.createHmac()</code></a>方法用来创建<code>Hmac</code>实例。<code>Hmac</code>不能直接使用<code>new</code>关键字创建对象。</p>
<p>示例:使用<code>Hmac</code>对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const hmac = crypto.createHmac(&apos;sha256&apos;, &apos;a secret&apos;);

hmac.on(&apos;readable&apos;, () =&gt; {
  const data = hmac.read();
  if (data) {
    console.log(data.toString(&apos;hex&apos;));
    // Prints:
    //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
  }
});

hmac.write(&apos;some data to hash&apos;);
hmac.end();
</code></pre>
<p>示例：使用<code>Hmac</code>和管道流</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);
const hmac = crypto.createHmac(&apos;sha256&apos;, &apos;a secret&apos;);

const input = fs.createReadStream(&apos;test.js&apos;);
input.pipe(hmac).pipe(process.stdout);
</code></pre>
<p>示例：使用<a href="#crypto_hmac_update_data_input_encoding"><code>hmac.update()</code></a>和<a href="#crypto_hmac_digest_encoding"><code>hmac.digest()</code></a>方法</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const hmac = crypto.createHmac(&apos;sha256&apos;, &apos;a secret&apos;);

hmac.update(&apos;some data to hash&apos;);
console.log(hmac.digest(&apos;hex&apos;));
// Prints:
//   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
</code></pre>
<h3>hmac.digest([encoding])<span><a class="mark" href="#crypto_hmac_digest_encoding" id="crypto_hmac_digest_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>encoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Calculates the HMAC digest of all of the data passed using <a href="#crypto_hmac_update_data_input_encoding"><code>hmac.update()</code></a>.
The <code>encoding</code> can be <code>&apos;hex&apos;</code>, <code>&apos;latin1&apos;</code> or <code>&apos;base64&apos;</code>. If <code>encoding</code> is
provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned;</p>
<p>The <code>Hmac</code> object can not be used again after <code>hmac.digest()</code> has been
called. Multiple calls to <code>hmac.digest()</code> will result in an error being thrown.</p>
<h3>hmac.update(data[, inputEncoding])<span><a class="mark" href="#crypto_hmac_update_data_inputencoding" id="crypto_hmac_update_data_inputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.1.94</td>
<td><p><span>新增于: v0.1.94</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Updates the <code>Hmac</code> content with the given <code>data</code>, the encoding of which
is given in <code>inputEncoding</code> and can be <code>&apos;utf8&apos;</code>, <code>&apos;ascii&apos;</code> or
<code>&apos;latin1&apos;</code>. If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
encoding of <code>&apos;utf8&apos;</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
<p>This can be called many times with new data as it is streamed.</p>
<h2>Class: Sign<span><a class="mark" href="#crypto_class_sign" id="crypto_class_sign">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><p>“Sign”类是生成签名的实用工具。它有两种使用方式:</p>
<ul>
<li>作为一个可写的<a href="stream.html">stream</a>，在这里，要签署的数据是写出来的，<a href="#crypto_sign_sign_private_key_output_format"><code>sign.sign()</code></a>方法用于生成并返回签名</li>
<li>使用<a href="#crypto_sign_update_data_input_encoding"><code>sign.update()</code></a>和<a href="#crypto_sign_sign_private_key_output_format"><code>sign.sign()</code></a>方法生产签名。</li>
</ul>
<p><a href="#crypto_crypto_createsign_algorithm"><code>crypto.createSign()</code></a>方法用于创建<code>Sign</code>实例。<code>Sign</code>实例不能直接使用<code>new</code>关键字创建。</p>
<p>示例:使用“符号”对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const sign = crypto.createSign(&apos;SHA256&apos;);

sign.write(&apos;some data to sign&apos;);
sign.end();

const privateKey = getPrivateKeySomehow();
console.log(sign.sign(privateKey, &apos;hex&apos;));
// Prints: the calculated signature using the specified private key and
// SHA-256. For RSA keys, the algorithm is RSASSA-PKCS1-v1_5 (see padding
// parameter below for RSASSA-PSS). For EC keys, the algorithm is ECDSA.
</code></pre>
<p>示例：使用<a href="#crypto_sign_update_data_input_encoding"><code>sign.update()</code></a>和<a href="#crypto_sign_sign_private_key_output_format"><code>sign.sign()</code></a>方法：</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const sign = crypto.createSign(&apos;SHA256&apos;);

sign.update(&apos;some data to sign&apos;);

const privateKey = getPrivateKeySomehow();
console.log(sign.sign(privateKey, &apos;hex&apos;));
// Prints: the calculated signature
</code></pre>
<p>一个<code>Sign</code>实例也可以通过仅仅通过摘要来创建算法名称，在这种情况下，OpenSSL将会从PEM-formatted私钥的类型推断出完整的签名算法，包括不直接暴露姓名常数的算法。比如&apos;ecdsa-with-SHA256&apos;。</p>
<p>示例:使用ECDSA与SHA256进行签名</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const sign = crypto.createSign(&apos;RSA-SHA256&apos;);

sign.update(&apos;some data to sign&apos;);

const privateKey = getPrivateKeySomehow();
console.log(sign.sign(privateKey, &apos;hex&apos;));
// Prints: the calculated signature
</code></pre>
<h3>sign.sign(privateKey[, outputFormat])<span><a class="mark" href="#crypto_sign_sign_privatekey_outputformat" id="crypto_sign_sign_privatekey_outputformat">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p>Support for RSASSA-PSS and additional options was added.</p>
</td></tr>
<tr><td>v0.1.92</td>
<td><p><span>新增于: v0.1.92</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>privateKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>passphrase</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</li>
<li><code>outputFormat</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Calculates the signature on all the data passed through using either
<a href="#crypto_sign_update_data_input_encoding"><code>sign.update()</code></a> or <a href="stream.html#stream_writable_write_chunk_encoding_callback"><code>sign.write()</code></a>.</p>
<p>The <code>privateKey</code> argument can be an object or a string. If <code>privateKey</code> is a
string, it is treated as a raw key with no passphrase. If <code>privateKey</code> is an
object, it must contain one or more of the following properties:</p>
<ul>
<li><code>key</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> - PEM encoded private key (required)</li>
<li><code>passphrase</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> - passphrase for the private key</li>
<li><p><code>padding</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> - Optional padding value for RSA, one of the following:</p>
<ul>
<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
</ul>
<p>Note that <code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
</li>
<li><code>saltLength</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> - salt length for when padding is
<code>RSA_PKCS1_PSS_PADDING</code>. The special value
<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the
maximum permissible value.</li>
</ul>
<p>The <code>outputFormat</code> can specify one of <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code> or <code>&apos;base64&apos;</code>. If
<code>outputFormat</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is
returned.</p>
<p>The <code>Sign</code> object can not be again used after <code>sign.sign()</code> method has been
called. Multiple calls to <code>sign.sign()</code> will result in an error being thrown.</p>
<h3>sign.update(data[, inputEncoding])<span><a class="mark" href="#crypto_sign_update_data_inputencoding" id="crypto_sign_update_data_inputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.1.92</td>
<td><p><span>新增于: v0.1.92</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Updates the <code>Sign</code> content with the given <code>data</code>, the encoding of which
is given in <code>inputEncoding</code> and can be <code>&apos;utf8&apos;</code>, <code>&apos;ascii&apos;</code> or
<code>&apos;latin1&apos;</code>. If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
encoding of <code>&apos;utf8&apos;</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
<p>This can be called many times with new data as it is streamed.</p>
<h2>Class: Verify<span><a class="mark" href="#crypto_class_verify" id="crypto_class_verify">#</a></span></h2>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><p><code>Verify</code>类是验证签名的工具。它可以两种方式使用:</p>
<ul>
<li>作为可写的<a href="stream.html">stream</a>，使用书面数据来验证提供的签名</li>
<li>使用<a href="#crypto_verifier_update_data_input_encoding"><code>verify.update()</code></a>和<a href="#crypto_verifier_verify_object_signature_signature_format"><code>verify.verify()</code></a>的方法来验证签名。</li>
</ul>
<p><a href="#crypto_crypto_createverify_algorithm"><code>crypto.createVerify()</code></a>方法用于创建<code>Verify</code>实例。
<code>Verify</code>对象不能直接使用<code>new</code>关键字创建。</p>
<p>示例:使用“验证”对象作为流:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const verify = crypto.createVerify(&apos;SHA256&apos;);

verify.write(&apos;some data to sign&apos;);
verify.end();

const publicKey = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(verify.verify(publicKey, signature));
// Prints: true or false
</code></pre>
<p>示例：使用<a href="#crypto_verifier_update_data_input_encoding"><code>verify.update()</code></a>和<a href="#crypto_verifier_verify_object_signature_signature_format"><code>verify.verify()</code></a>方法</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const verify = crypto.createVerify(&apos;SHA256&apos;);

verify.update(&apos;some data to sign&apos;);

const publicKey = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(verify.verify(publicKey, signature));
// Prints: true or false
</code></pre>
<h3>verify.update(data[, inputEncoding])<span><a class="mark" href="#crypto_verify_update_data_inputencoding" id="crypto_verify_update_data_inputencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.1.92</td>
<td><p><span>新增于: v0.1.92</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>data</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>inputEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Updates the <code>Verify</code> content with the given <code>data</code>, the encoding of which
is given in <code>inputEncoding</code> and can be <code>&apos;utf8&apos;</code>, <code>&apos;ascii&apos;</code> or
<code>&apos;latin1&apos;</code>. If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
encoding of <code>&apos;utf8&apos;</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
<p>This can be called many times with new data as it is streamed.</p>
<h3>verify.verify(object, signature[, signatureFormat])<span><a class="mark" href="#crypto_verify_verify_object_signature_signatureformat" id="crypto_verify_verify_object_signature_signatureformat">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p>Support for RSASSA-PSS and additional options was added.</p>
</td></tr>
<tr><td>v0.1.92</td>
<td><p><span>新增于: v0.1.92</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>object</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></li>
<li><code>signature</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>signatureFormat</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Verifies the provided data using the given <code>object</code> and <code>signature</code>.
The <code>object</code> argument can be either a string containing a PEM encoded object,
which can be an RSA public key, a DSA public key, or an X.509 certificate,
or an object with one or more of the following properties:</p>
<ul>
<li><code>key</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> - PEM encoded public key (required)</li>
<li><p><code>padding</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> - Optional padding value for RSA, one of the following:</p>
<ul>
<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
</ul>
<p>Note that <code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
used to verify the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
</li>
<li><code>saltLength</code>: 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> - salt length for when padding is
<code>RSA_PKCS1_PSS_PADDING</code>. The special value
<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
size, <code>crypto.constants.RSA_PSS_SALTLEN_AUTO</code> (default) causes it to be
determined automatically.</li>
</ul>
<p>The <code>signature</code> argument is the previously calculated signature for the data, in
the <code>signatureFormat</code> which can be <code>&apos;latin1&apos;</code>, <code>&apos;hex&apos;</code> or <code>&apos;base64&apos;</code>.
If a <code>signatureFormat</code> is specified, the <code>signature</code> is expected to be a
string; otherwise <code>signature</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
<code>TypedArray</code>, or <code>DataView</code>.</p>
<p>Returns <code>true</code> or <code>false</code> depending on the validity of the signature for
the data and public key.</p>
<p>The <code>verify</code> object can not be used again after <code>verify.verify()</code> has been
called. Multiple calls to <code>verify.verify()</code> will result in an error being
thrown.</p>
<h2><code>crypto</code> module methods and properties<span><a class="mark" href="#crypto_crypto_module_methods_and_properties" id="crypto_crypto_module_methods_and_properties">#</a></span></h2>
<h3>crypto.constants<span><a class="mark" href="#crypto_crypto_constants" id="crypto_crypto_constants">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v6.3.0</span>
</div><p>Returns an object containing commonly used constants for crypto and security
related operations. The specific constants currently defined are described in
<a href="#crypto_crypto_constants_1">Crypto Constants</a>.</p>
<h3>crypto.DEFAULT_ENCODING<span><a class="mark" href="#crypto_crypto_default_encoding" id="crypto_crypto_default_encoding">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.9.3</span>
</div><p>The default encoding to use for functions that can take either strings
or <a href="buffer.html">buffers</a>. The default value is <code>&apos;buffer&apos;</code>, which makes methods
default to <a href="buffer.html"><code>Buffer</code></a> objects.</p>
<p>The <code>crypto.DEFAULT_ENCODING</code> mechanism is provided for backwards compatibility
with legacy programs that expect <code>&apos;latin1&apos;</code> to be the default encoding.</p>
<p>New applications should expect the default to be <code>&apos;buffer&apos;</code>. This property may
become deprecated in a future Node.js release.</p>
<h3>crypto.fips<span><a class="mark" href="#crypto_crypto_fips" id="crypto_crypto_fips">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v6.0.0</span>
</div><p>Property for checking and controlling whether a FIPS compliant crypto provider is
currently in use. Setting to true requires a FIPS build of Node.js.</p>
<h3>crypto.createCipher(algorithm, password[, options])<span><a class="mark" href="#crypto_crypto_createcipher_algorithm_password_options" id="crypto_crypto_createcipher_algorithm_password_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>password</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
<p>Creates and returns a <code>Cipher</code> object that uses the given <code>algorithm</code> and
<code>password</code>. Optional <code>options</code> argument controls stream behavior.</p>
<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>&apos;aes192&apos;</code>, etc. On
recent OpenSSL releases, <code>openssl list-cipher-algorithms</code> will display the
available cipher algorithms.</p>
<p>The <code>password</code> is used to derive the cipher key and initialization vector (IV).
The value must be either a <code>&apos;latin1&apos;</code> encoded string, a <a href="buffer.html"><code>Buffer</code></a>, a
<code>TypedArray</code>, or a <code>DataView</code>.</p>
<p>The implementation of <code>crypto.createCipher()</code> derives keys using the OpenSSL
function <a href="https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one
iteration, and no salt. The lack of salt allows dictionary attacks as the same
password always creates the same key. The low iteration count and
non-cryptographically secure hash algorithm allow passwords to be tested very
rapidly.</p>
<p>In line with OpenSSL&apos;s recommendation to use PBKDF2 instead of
<a href="https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on
their own using <a href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback"><code>crypto.pbkdf2()</code></a> and to use <a href="#crypto_crypto_createcipheriv_algorithm_key_iv"><code>crypto.createCipheriv()</code></a>
to create the <code>Cipher</code> object. Users should not use ciphers with counter mode
(e.g. CTR, GCM, or CCM) in <code>crypto.createCipher()</code>. A warning is emitted when
they are used in order to avoid the risk of IV reuse that causes
vulnerabilities. For the case when IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disrespecting
Adversaries</a> for details.</p>
<h3>crypto.createCipheriv(algorithm, key, iv[, options])<span><a class="mark" href="#crypto_crypto_createcipheriv_algorithm_key_iv_options" id="crypto_crypto_createcipheriv_algorithm_key_iv_options">#</a></span></h3>
<div class="signature"><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>iv</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
</div><p>Creates and returns a <code>Cipher</code> object, with the given <code>algorithm</code>, <code>key</code> and
initialization vector (<code>iv</code>). Optional <code>options</code> argument controls stream behavior.</p>
<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>&apos;aes192&apos;</code>, etc. On
recent OpenSSL releases, <code>openssl list-cipher-algorithms</code> will display the
available cipher algorithms.</p>
<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an
<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>&apos;utf8&apos;</code> encoded strings,
<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s.</p>
<h3>crypto.createCredentials(details)<span><a class="mark" href="#crypto_crypto_createcredentials_details" id="crypto_crypto_createcredentials_details">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.92</span><span>废弃于: v0.11.13</span>
</div><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="tls.html#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> instead.</div><ul>
<li><code>details</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> Identical to <a href="tls.html#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>.</li>
</ul>
<p>The <code>crypto.createCredentials()</code> method is a deprecated function for creating
and returning a <code>tls.SecureContext</code>. It should not be used. Replace it with
<a href="tls.html#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> which has the exact same arguments and return
value.</p>
<p>Returns a <code>tls.SecureContext</code>, as-if <a href="tls.html#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> had been
called.</p>
<h3>crypto.createDecipher(algorithm, password[, options])<span><a class="mark" href="#crypto_crypto_createdecipher_algorithm_password_options" id="crypto_crypto_createdecipher_algorithm_password_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>password</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code> and
<code>password</code> (key). Optional <code>options</code> argument controls stream behavior.</p>
<p>The implementation of <code>crypto.createDecipher()</code> derives keys using the OpenSSL
function <a href="https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one
iteration, and no salt. The lack of salt allows dictionary attacks as the same
password always creates the same key. The low iteration count and
non-cryptographically secure hash algorithm allow passwords to be tested very
rapidly.</p>
<p>In line with OpenSSL&apos;s recommendation to use PBKDF2 instead of
<a href="https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on
their own using <a href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback"><code>crypto.pbkdf2()</code></a> and to use <a href="#crypto_crypto_createdecipheriv_algorithm_key_iv"><code>crypto.createDecipheriv()</code></a>
to create the <code>Decipher</code> object.</p>
<h3>crypto.createDecipheriv(algorithm, key, iv[, options])<span><a class="mark" href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options" id="crypto_crypto_createdecipheriv_algorithm_key_iv_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>iv</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code>, <code>key</code>
and initialization vector (<code>iv</code>). Optional <code>options</code> argument controls stream
behavior.</p>
<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>&apos;aes192&apos;</code>, etc. On
recent OpenSSL releases, <code>openssl list-cipher-algorithms</code> will display the
available cipher algorithms.</p>
<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an
<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>&apos;utf8&apos;</code> encoded strings or
<a href="buffer.html">buffers</a>.</p>
<h3>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])<span><a class="mark" href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding" id="crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>prime</code> argument can be any <code>TypedArray</code> or <code>DataView</code> now.</p>
</td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>prime</code> argument can be a <code>Uint8Array</code> now.</p>
</td></tr>
<tr><td>v6.0.0</td>
<td><p>The default for the encoding parameters changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.11.12</td>
<td><p><span>新增于: v0.11.12</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>prime</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>primeEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>generator</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a> Defaults to <code>2</code>.</li>
<li><code>generatorEncoding</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Creates a <code>DiffieHellman</code> key exchange object using the supplied <code>prime</code> and an
optional specific <code>generator</code>.</p>
<p>The <code>generator</code> argument can be a number, string, or <a href="buffer.html"><code>Buffer</code></a>. If
<code>generator</code> is not specified, the value <code>2</code> is used.</p>
<p>The <code>primeEncoding</code> and <code>generatorEncoding</code> arguments can be <code>&apos;latin1&apos;</code>,
<code>&apos;hex&apos;</code>, or <code>&apos;base64&apos;</code>.</p>
<p>If <code>primeEncoding</code> is specified, <code>prime</code> is expected to be a string; otherwise
a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
<p>If <code>generatorEncoding</code> is specified, <code>generator</code> is expected to be a string;
otherwise a number, <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
<h3>crypto.createDiffieHellman(primeLength[, generator])<span><a class="mark" href="#crypto_crypto_creatediffiehellman_primelength_generator" id="crypto_crypto_creatediffiehellman_primelength_generator">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.0</span>
</div><ul>
<li><code>primeLength</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>generator</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a> Defaults to <code>2</code>.</li>
</ul>
<p>Creates a <code>DiffieHellman</code> key exchange object and generates a prime of
<code>primeLength</code> bits using an optional specific numeric <code>generator</code>.
If <code>generator</code> is not specified, the value <code>2</code> is used.</p>
<h3>crypto.createECDH(curveName)<span><a class="mark" href="#crypto_crypto_createecdh_curvename" id="crypto_crypto_createecdh_curvename">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>curveName</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a
predefined curve specified by the <code>curveName</code> string. Use
<a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. On recent
OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name
and description of each available elliptic curve.</p>
<h3>crypto.createHash(algorithm[, options])<span><a class="mark" href="#crypto_crypto_createhash_algorithm_options" id="crypto_crypto_createhash_algorithm_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
<p>Creates and returns a <code>Hash</code> object that can be used to generate hash digests
using the given <code>algorithm</code>. Optional <code>options</code> argument controls stream
behavior.</p>
<p>The <code>algorithm</code> is dependent on the available algorithms supported by the
version of OpenSSL on the platform. Examples are <code>&apos;sha256&apos;</code>, <code>&apos;sha512&apos;</code>, etc.
On recent releases of OpenSSL, <code>openssl list-message-digest-algorithms</code> will
display the available digest algorithms.</p>
<p>Example: generating the sha256 sum of a file</p>
<pre><code class="lang-js">const filename = process.argv[2];
const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);

const hash = crypto.createHash(&apos;sha256&apos;);

const input = fs.createReadStream(filename);
input.on(&apos;readable&apos;, () =&gt; {
  const data = input.read();
  if (data)
    hash.update(data);
  else {
    console.log(`${hash.digest(&apos;hex&apos;)} ${filename}`);
  }
});
</code></pre>
<h3>crypto.createHmac(algorithm, key[, options])<span><a class="mark" href="#crypto_crypto_createhmac_algorithm_key_options" id="crypto_crypto_createhmac_algorithm_key_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.94</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
</ul>
<p>Creates and returns an <code>Hmac</code> object that uses the given <code>algorithm</code> and <code>key</code>.
Optional <code>options</code> argument controls stream behavior.</p>
<p>The <code>algorithm</code> is dependent on the available algorithms supported by the
version of OpenSSL on the platform. Examples are <code>&apos;sha256&apos;</code>, <code>&apos;sha512&apos;</code>, etc.
On recent releases of OpenSSL, <code>openssl list-message-digest-algorithms</code> will
display the available digest algorithms.</p>
<p>The <code>key</code> is the HMAC key used to generate the cryptographic HMAC hash.</p>
<p>Example: generating the sha256 HMAC of a file</p>
<pre><code class="lang-js">const filename = process.argv[2];
const crypto = require(&apos;crypto&apos;);
const fs = require(&apos;fs&apos;);

const hmac = crypto.createHmac(&apos;sha256&apos;, &apos;a secret&apos;);

const input = fs.createReadStream(filename);
input.on(&apos;readable&apos;, () =&gt; {
  const data = input.read();
  if (data)
    hmac.update(data);
  else {
    console.log(`${hmac.digest(&apos;hex&apos;)} ${filename}`);
  }
});
</code></pre>
<h3>crypto.createSign(algorithm[, options])<span><a class="mark" href="#crypto_crypto_createsign_algorithm_options" id="crypto_crypto_createsign_algorithm_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_constructor_new_stream_writable_options"><code>stream.Writable</code> options</a></li>
</ul>
<p>Creates and returns a <code>Sign</code> object that uses the given <code>algorithm</code>.
Use <a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available
signing algorithms. Optional <code>options</code> argument controls the
<code>stream.Writable</code> behavior.</p>
<h3>crypto.createVerify(algorithm[, options])<span><a class="mark" href="#crypto_crypto_createverify_algorithm_options" id="crypto_crypto_createverify_algorithm_options">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.1.92</span>
</div><ul>
<li><code>algorithm</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>options</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> <a href="stream.html#stream_constructor_new_stream_writable_options"><code>stream.Writable</code> options</a></li>
</ul>
<p>Creates and returns a <code>Verify</code> object that uses the given algorithm.
Use <a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available
signing algorithms. Optional <code>options</code> argument controls the
<code>stream.Writable</code> behavior.</p>
<h3>crypto.getCiphers()<span><a class="mark" href="#crypto_crypto_getciphers" id="crypto_crypto_getciphers">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.9.3</span>
</div><p>Returns an array with the names of the supported cipher algorithms.</p>
<p>Example:</p>
<pre><code class="lang-js">const ciphers = crypto.getCiphers();
console.log(ciphers); // [&apos;aes-128-cbc&apos;, &apos;aes-128-ccm&apos;, ...]
</code></pre>
<h3>crypto.getCurves()<span><a class="mark" href="#crypto_crypto_getcurves" id="crypto_crypto_getcurves">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v2.3.0</span>
</div><p>Returns an array with the names of the supported elliptic curves.</p>
<p>Example:</p>
<pre><code class="lang-js">const curves = crypto.getCurves();
console.log(curves); // [&apos;Oakley-EC2N-3&apos;, &apos;Oakley-EC2N-4&apos;, ...]
</code></pre>
<h3>crypto.getDiffieHellman(groupName)<span><a class="mark" href="#crypto_crypto_getdiffiehellman_groupname" id="crypto_crypto_getdiffiehellman_groupname">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.7.5</span>
</div><ul>
<li><code>groupName</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Creates a predefined <code>DiffieHellman</code> key exchange object. The
supported groups are: <code>&apos;modp1&apos;</code>, <code>&apos;modp2&apos;</code>, <code>&apos;modp5&apos;</code> (defined in
<a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a>, but see <a href="#crypto_support_for_weak_or_compromised_algorithms">Caveats</a>) and <code>&apos;modp14&apos;</code>, <code>&apos;modp15&apos;</code>,
<code>&apos;modp16&apos;</code>, <code>&apos;modp17&apos;</code>, <code>&apos;modp18&apos;</code> (defined in <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a>). The
returned object mimics the interface of objects created by
<a href="#crypto_crypto_creatediffiehellman_prime_prime_encoding_generator_generator_encoding"><code>crypto.createDiffieHellman()</code></a>, but will not allow changing
the keys (with <a href="#crypto_diffiehellman_setpublickey_public_key_encoding"><code>diffieHellman.setPublicKey()</code></a> for example). The
advantage of using this method is that the parties do not have to
generate nor exchange a group modulus beforehand, saving both processor
and communication time.</p>
<p>Example (obtaining a shared secret):</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const alice = crypto.getDiffieHellman(&apos;modp14&apos;);
const bob = crypto.getDiffieHellman(&apos;modp14&apos;);

alice.generateKeys();
bob.generateKeys();

const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, &apos;hex&apos;);
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, &apos;hex&apos;);

/* aliceSecret and bobSecret should be the same */
console.log(aliceSecret === bobSecret);
</code></pre>
<h3>crypto.getHashes()<span><a class="mark" href="#crypto_crypto_gethashes" id="crypto_crypto_gethashes">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.9.3</span>
</div><p>Returns an array of the names of the supported hash algorithms,
such as <code>RSA-SHA256</code>.</p>
<p>Example:</p>
<pre><code class="lang-js">const hashes = crypto.getHashes();
console.log(hashes); // [&apos;DSA&apos;, &apos;DSA-SHA&apos;, &apos;DSA-SHA1&apos;, ...]
</code></pre>
<h3>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)<span><a class="mark" href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback" id="crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>digest</code> parameter is always required now.</p>
</td></tr>
<tr><td>v6.0.0</td>
<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p>
</td></tr>
<tr><td>v6.0.0</td>
<td><p>The default encoding for <code>password</code> if it is a string changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.5.5</td>
<td><p><span>新增于: v0.5.5</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>password</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>salt</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>iterations</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>keylen</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>digest</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>callback</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a><ul>
<li><code>err</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&lt;Error&gt;</a></li>
<li><code>derivedKey</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
implementation. A selected HMAC digest algorithm specified by <code>digest</code> is
applied to derive a key of the requested byte length (<code>keylen</code>) from the
<code>password</code>, <code>salt</code> and <code>iterations</code>.</p>
<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and
<code>derivedKey</code>. If an error occurs, <code>err</code> will be set; otherwise <code>err</code> will be
null. The successfully generated <code>derivedKey</code> will be passed as a <a href="buffer.html"><code>Buffer</code></a>.</p>
<p>The <code>iterations</code> argument must be a number set as high as possible. The
higher the number of iterations, the more secure the derived key will be,
but will take a longer amount of time to complete.</p>
<p>The <code>salt</code> should also be as unique as possible. It is recommended that the
salts are random and their lengths are at least 16 bytes. See
<a href="http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf">NIST SP 800-132</a> for details.</p>
<p>Example:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
crypto.pbkdf2(&apos;secret&apos;, &apos;salt&apos;, 100000, 64, &apos;sha512&apos;, (err, derivedKey) =&gt; {
  if (err) throw err;
  console.log(derivedKey.toString(&apos;hex&apos;));  // &apos;3745e48...08d59ae&apos;
});
</code></pre>
<p>An array of supported digest functions can be retrieved using
<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p>
<p>Note that this API uses libuv&apos;s threadpool, which can have surprising and
negative performance implications for some applications, see the
<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
<h3>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)<span><a class="mark" href="#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest" id="crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v6.0.0</td>
<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p>
</td></tr>
<tr><td>v6.0.0</td>
<td><p>The default encoding for <code>password</code> if it is a string changed from <code>binary</code> to <code>utf8</code>.</p>
</td></tr>
<tr><td>v0.9.3</td>
<td><p><span>新增于: v0.9.3</span></p>
</td></tr>
</tbody></table>
</details>
</div><ul>
<li><code>password</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>salt</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>iterations</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>keylen</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>digest</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
implementation. A selected HMAC digest algorithm specified by <code>digest</code> is
applied to derive a key of the requested byte length (<code>keylen</code>) from the
<code>password</code>, <code>salt</code> and <code>iterations</code>.</p>
<p>If an error occurs an Error will be thrown, otherwise the derived key will be
returned as a <a href="buffer.html"><code>Buffer</code></a>.</p>
<p>The <code>iterations</code> argument must be a number set as high as possible. The
higher the number of iterations, the more secure the derived key will be,
but will take a longer amount of time to complete.</p>
<p>The <code>salt</code> should also be as unique as possible. It is recommended that the
salts are random and their lengths are at least 16 bytes. See
<a href="http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf">NIST SP 800-132</a> for details.</p>
<p>Example:</p>
<pre><code class="lang-js">const crypto = require(&apos;crypto&apos;);
const key = crypto.pbkdf2Sync(&apos;secret&apos;, &apos;salt&apos;, 100000, 64, &apos;sha512&apos;);
console.log(key.toString(&apos;hex&apos;));  // &apos;3745e48...08d59ae&apos;
</code></pre>
<p>An array of supported digest functions can be retrieved using
<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p>
<h3>crypto.privateDecrypt(privateKey, buffer)<span><a class="mark" href="#crypto_crypto_privatedecrypt_privatekey_buffer" id="crypto_crypto_privatedecrypt_privatekey_buffer">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>privateKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> A PEM encoded private key.</li>
<li><code>passphrase</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> An optional passphrase for the private key.</li>
<li><code>padding</code> <span class="type">&lt;crypto.constants&gt;</span> An optional padding value defined in
<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>,
<code>RSA_PKCS1_PADDING</code>, or <code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li>
</ul>
</li>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>Returns: 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> A new <code>Buffer</code> with the decrypted content.</li>
</ul>
<p>Decrypts <code>buffer</code> with <code>privateKey</code>.</p>
<p><code>privateKey</code> can be an object or a string. If <code>privateKey</code> is a string, it is
treated as the key with no passphrase and will use <code>RSA_PKCS1_OAEP_PADDING</code>.</p>
<h3>crypto.privateEncrypt(privateKey, buffer)<span><a class="mark" href="#crypto_crypto_privateencrypt_privatekey_buffer" id="crypto_crypto_privateencrypt_privatekey_buffer">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v1.1.0</span>
</div><ul>
<li><code>privateKey</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> A PEM encoded private key.</li>
<li><code>passphrase</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> An optional passphrase for the private key.</li>
<li><code>padding</code> <span class="type">&lt;crypto.constants&gt;</span> An optional padding value defined in
<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or
<code>RSA_PKCS1_PADDING</code>.</li>
</ul>
</li>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>Returns: 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> A new <code>Buffer</code> with the encrypted content.</li>
</ul>
<p>Encrypts <code>buffer</code> with <code>privateKey</code>.</p>
<p><code>privateKey</code> can be an object or a string. If <code>privateKey</code> is a string, it is
treated as the key with no passphrase and will use <code>RSA_PKCS1_PADDING</code>.</p>
<h3>crypto.publicDecrypt(key, buffer)<span><a class="mark" href="#crypto_crypto_publicdecrypt_key_buffer" id="crypto_crypto_publicdecrypt_key_buffer">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v1.1.0</span>
</div><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> A PEM encoded public or private key.</li>
<li><code>passphrase</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> An optional passphrase for the private key.</li>
<li><code>padding</code> <span class="type">&lt;crypto.constants&gt;</span> An optional padding value defined in
<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or
<code>RSA_PKCS1_PADDING</code>.</li>
</ul>
</li>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>Returns: 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> A new <code>Buffer</code> with the decrypted content.</li>
</ul>
<p>Decrypts <code>buffer</code> with <code>key</code>.</p>
<p><code>key</code> can be an object or a string. If <code>key</code> is a string, it is treated as
the key with no passphrase and will use <code>RSA_PKCS1_PADDING</code>.</p>
<p>Because RSA public keys can be derived from private keys, a private key may
be passed instead of a public key.</p>
<h3>crypto.publicEncrypt(key, buffer)<span><a class="mark" href="#crypto_crypto_publicencrypt_key_buffer" id="crypto_crypto_publicencrypt_key_buffer">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.14</span>
</div><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a><ul>
<li><code>key</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> A PEM encoded public or private key.</li>
<li><code>passphrase</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> An optional passphrase for the private key.</li>
<li><code>padding</code> <span class="type">&lt;crypto.constants&gt;</span> An optional padding value defined in
<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>,
<code>RSA_PKCS1_PADDING</code>, or <code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li>
</ul>
</li>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li>Returns: 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> A new <code>Buffer</code> with the encrypted content.</li>
</ul>
<p>Encrypts the content of <code>buffer</code> with <code>key</code> and returns a new
<a href="buffer.html"><code>Buffer</code></a> with encrypted content.</p>
<p><code>key</code> can be an object or a string. If <code>key</code> is a string, it is treated as
the key with no passphrase and will use <code>RSA_PKCS1_OAEP_PADDING</code>.</p>
<p>Because RSA public keys can be derived from private keys, a private key may
be passed instead of a public key.</p>
<h3>crypto.randomBytes(size[, callback])<span><a class="mark" href="#crypto_crypto_randombytes_size_callback" id="crypto_crypto_randombytes_size_callback">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.5.8</span>
</div><ul>
<li><code>size</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>callback</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a><ul>
<li><code>err</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&lt;Error&gt;</a></li>
<li><code>buf</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>生成加密强伪随机数据. <code>size</code>参数是指示要生成的字节数的数值。</p>
<p>如果提供 <code>callback</code>回调函数 ,这些字节是异步生成的并且使用两个参数调用<code>callback</code>函数：<code>err</code>和<code>buf</code>。
如果发生错误, <code>err</code>是一个Error对象； 否则为null。<code>buf</code>参数是包含生成字节的<a href="buffer.html"><code>Buffer</code></a>。</p>
<pre><code class="lang-js">// Asynchronous
const crypto = require(&apos;crypto&apos;);
crypto.randomBytes(256, (err, buf) =&gt; {
  if (err) throw err;
  console.log(`${buf.length} bytes of random data: ${buf.toString(&apos;hex&apos;)}`);
});
</code></pre>
<p>如果未提供 <code>callback</code>回调函数, 则同步地生成随机字节并返回为<a href="buffer.html"><code>Buffer</code></a>。如果生成字节遇到问题，将会抛出一个错误。</p>
<pre><code class="lang-js">// Synchronous
const buf = crypto.randomBytes(256);
console.log(
  `${buf.length} bytes of random data: ${buf.toString(&apos;hex&apos;)}`);
</code></pre>
<p><code>crypto.randomBytes()</code>方法将在获得足够的熵之后完成。这通常不会超过几毫秒。只有在刚开机时才可能会阻塞更久，因为此时整个系统的熵不多。</p>
<p>注意这个API使用libuv的线程池，所以在某些时候可能会产生意外的性能问题，查看<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a>的文档以了解更多信息。</p>
<p><em>Note</em>: The asynchronous version of <code>crypto.randomBytes()</code> is carried out
in a single threadpool request. To minimize threadpool task length variation,
partition large <code>randomBytes</code> requests when doing so as part of fulfilling a
client request.</p>
<h3>crypto.randomFillSync(buffer[, offset][, size])<span><a class="mark" href="#crypto_crypto_randomfillsync_buffer_offset_size" id="crypto_crypto_randomfillsync_buffer_offset_size">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v7.10.0</span>
</div><ul>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&lt;Uint8Array&gt;</a> Must be supplied.</li>
<li><code>offset</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Defaults to <code>0</code>.</li>
<li><code>size</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Defaults to <code>buffer.length - offset</code>.</li>
</ul>
<p>Synchronous version of <a href="#crypto_crypto_randomfill_buffer_offset_size_callback"><code>crypto.randomFill()</code></a>.</p>
<p>Returns <code>buffer</code></p>
<pre><code class="lang-js">const buf = Buffer.alloc(10);
console.log(crypto.randomFillSync(buf).toString(&apos;hex&apos;));

crypto.randomFillSync(buf, 5);
console.log(buf.toString(&apos;hex&apos;));

// The above is equivalent to the following:
crypto.randomFillSync(buf, 5, 5);
console.log(buf.toString(&apos;hex&apos;));
</code></pre>
<h3>crypto.randomFill(buffer[, offset][, size], callback)<span><a class="mark" href="#crypto_crypto_randomfill_buffer_offset_size_callback" id="crypto_crypto_randomfill_buffer_offset_size_callback">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v7.10.0</span>
</div><ul>
<li><code>buffer</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&lt;Uint8Array&gt;</a>必须被支持.</li>
<li><code>offset</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Defaults to <code>0</code>.</li>
<li><code>size</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Defaults to <code>buffer.length - offset</code>.</li>
<li><code>callback</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> <code>function(err, buf) {}</code>.</li>
</ul>
<p>This function is similar to <a href="#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes()</code></a> but requires the first
argument to be a <a href="buffer.html"><code>Buffer</code></a> that will be filled. It also
requires that a callback is passed in.</p>
<p>If the <code>callback</code> function is not provided, an error will be thrown.</p>
<pre><code class="lang-js">const buf = Buffer.alloc(10);
crypto.randomFill(buf, (err, buf) =&gt; {
  if (err) throw err;
  console.log(buf.toString(&apos;hex&apos;));
});

crypto.randomFill(buf, 5, (err, buf) =&gt; {
  if (err) throw err;
  console.log(buf.toString(&apos;hex&apos;));
});

// The above is equivalent to the following:
crypto.randomFill(buf, 5, 5, (err, buf) =&gt; {
  if (err) throw err;
  console.log(buf.toString(&apos;hex&apos;));
});
</code></pre>
<p>Note that this API uses libuv&apos;s threadpool, which can have surprising and
negative performance implications for some applications, see the
<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
<p><em>Note</em>: The asynchronous version of <code>crypto.randomFill()</code> is carried out
in a single threadpool request. To minimize threadpool task length variation,
partition large <code>randomFill</code> requests when doing so as part of fulfilling a
client request.</p>
<h3>crypto.setEngine(engine[, flags])<span><a class="mark" href="#crypto_crypto_setengine_engine_flags" id="crypto_crypto_setengine_engine_flags">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v0.11.11</span>
</div><ul>
<li><code>engine</code> 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>flags</code> <span class="type">&lt;crypto.constants&gt;</span> Defaults to <code>crypto.constants.ENGINE_METHOD_ALL</code>.</li>
</ul>
<p>Load and set the <code>engine</code> for some or all OpenSSL functions (selected by flags).</p>
<p><code>engine</code> could be either an id or a path to the engine&apos;s shared library.</p>
<p>The optional <code>flags</code> argument uses <code>ENGINE_METHOD_ALL</code> by default. The <code>flags</code>
is a bit field taking one of or a mix of the following flags (defined in
<code>crypto.constants</code>):</p>
<ul>
<li><code>crypto.constants.ENGINE_METHOD_RSA</code></li>
<li><code>crypto.constants.ENGINE_METHOD_DSA</code></li>
<li><code>crypto.constants.ENGINE_METHOD_DH</code></li>
<li><code>crypto.constants.ENGINE_METHOD_RAND</code></li>
<li><code>crypto.constants.ENGINE_METHOD_ECDH</code></li>
<li><code>crypto.constants.ENGINE_METHOD_ECDSA</code></li>
<li><code>crypto.constants.ENGINE_METHOD_CIPHERS</code></li>
<li><code>crypto.constants.ENGINE_METHOD_DIGESTS</code></li>
<li><code>crypto.constants.ENGINE_METHOD_STORE</code></li>
<li><code>crypto.constants.ENGINE_METHOD_PKEY_METHS</code></li>
<li><code>crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS</code></li>
<li><code>crypto.constants.ENGINE_METHOD_ALL</code></li>
<li><code>crypto.constants.ENGINE_METHOD_NONE</code></li>
</ul>
<h3>crypto.timingSafeEqual(a, b)<span><a class="mark" href="#crypto_crypto_timingsafeequal_a_b" id="crypto_crypto_timingsafeequal_a_b">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v6.6.0</span>
</div><ul>
<li><code>a</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
<li><code>b</code> 
            <a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a></li>
</ul>
<p>This function is based on a constant-time algorithm.
Returns true if <code>a</code> is equal to <code>b</code>, without leaking timing information that
would allow an attacker to guess one of the values. This is suitable for
comparing HMAC digests or secret values like authentication cookies or
<a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p>
<p><code>a</code> and <code>b</code> must both be <code>Buffer</code>s, <code>TypedArray</code>s, or <code>DataView</code>s, and they
must have the same length.</p>
<p><em>Note</em>: Use of <code>crypto.timingSafeEqual</code> does not guarantee that the
<em>surrounding</em> code is timing-safe. Care should be taken to ensure that the
surrounding code does not introduce timing vulnerabilities.</p>
<h2>Notes<span><a class="mark" href="#crypto_notes" id="crypto_notes">#</a></span></h2>
<h3>Legacy Streams API (pre Node.js v0.10)<span><a class="mark" href="#crypto_legacy_streams_api_pre_node_js_v0_10" id="crypto_legacy_streams_api_pre_node_js_v0_10">#</a></span></h3>
<p>The Crypto module was added to Node.js before there was the concept of a
unified Stream API, and before there were <a href="buffer.html"><code>Buffer</code></a> objects for handling
binary data. As such, the many of the <code>crypto</code> defined classes have methods not
typically found on other Node.js classes that implement the <a href="stream.html">streams</a>
API (e.g. <code>update()</code>, <code>final()</code>, or <code>digest()</code>). Also, many methods accepted
and returned <code>&apos;latin1&apos;</code> encoded strings by default rather than Buffers. This
default was changed after Node.js v0.8 to use <a href="buffer.html"><code>Buffer</code></a> objects by default
instead.</p>
<h3>Recent ECDH Changes<span><a class="mark" href="#crypto_recent_ecdh_changes" id="crypto_recent_ecdh_changes">#</a></span></h3>
<p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified.
Now, <a href="#crypto_ecdh_setprivatekey_private_key_encoding"><code>ecdh.setPrivateKey()</code></a> can be called with a preselected private key
and the associated public point (key) will be computed and stored in the object.
This allows code to only store and provide the private part of the EC key pair.
<a href="#crypto_ecdh_setprivatekey_private_key_encoding"><code>ecdh.setPrivateKey()</code></a> now also validates that the private key is valid for
the selected curve.</p>
<p>The <a href="#crypto_ecdh_setpublickey_public_key_encoding"><code>ecdh.setPublicKey()</code></a> method is now deprecated as its inclusion in the
API is not useful. Either a previously stored private key should be set, which
automatically generates the associated public key, or <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a>
should be called. The main drawback of using <a href="#crypto_ecdh_setpublickey_public_key_encoding"><code>ecdh.setPublicKey()</code></a> is that
it can be used to put the ECDH key pair into an inconsistent state.</p>
<h3>Support for weak or compromised algorithms<span><a class="mark" href="#crypto_support_for_weak_or_compromised_algorithms" id="crypto_support_for_weak_or_compromised_algorithms">#</a></span></h3>
<p>The <code>crypto</code> module still supports some algorithms which are already
compromised and are not currently recommended for use. The API also allows
the use of ciphers and hashes with a small key size that are considered to be
too weak for safe use.</p>
<p>Users should take full responsibility for selecting the crypto
algorithm and key size according to their security requirements.</p>
<p>Based on the recommendations of <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf">NIST SP 800-131A</a>:</p>
<ul>
<li>MD5 and SHA-1 are no longer acceptable where collision resistance is
required such as digital signatures.</li>
<li>The key used with RSA, DSA, and DH algorithms is recommended to have
at least 2048 bits and that of the curve of ECDSA and ECDH at least
224 bits, to be safe to use for several years.</li>
<li>The DH groups of <code>modp1</code>, <code>modp2</code> and <code>modp5</code> have a key size
smaller than 2048 bits and are not recommended.</li>
</ul>
<p>See the reference for other recommendations and details.</p>
<h2>Crypto Constants<span><a class="mark" href="#crypto_crypto_constants_1" id="crypto_crypto_constants_1">#</a></span></h2>
<p>The following constants exported by <code>crypto.constants</code> apply to various uses of
the <code>crypto</code>, <code>tls</code>, and <code>https</code> modules and are generally specific to OpenSSL.</p>
<h3>OpenSSL Options<span><a class="mark" href="#crypto_openssl_options" id="crypto_openssl_options">#</a></span></h3>
<table>
  <tbody><tr>
    <th>Constant</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><code>SSL_OP_ALL</code></td>
    <td>Applies multiple bug workarounds within OpenSSL. See
    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a> for
    detail.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td>
    <td>Allows legacy insecure renegotiation between OpenSSL and unpatched
    clients or servers. See
    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td>
    <td>Attempts to use the server&apos;s preferences instead of the client&apos;s when
    selecting a cipher. Behavior depends on protocol version. See
    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_CISCO_ANYCONNECT</code></td>
    <td>Instructs OpenSSL to use Cisco&apos;s &quot;speshul&quot; version of DTLS_BAD_VER.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_COOKIE_EXCHANGE</code></td>
    <td>Instructs OpenSSL to turn on cookie exchange.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td>
    <td>Instructs OpenSSL to add server-hello extension from an early version
    of the cryptopro draft.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td>
    <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability
    workaround added in OpenSSL 0.9.6d.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_EPHEMERAL_RSA</code></td>
    <td>Instructs OpenSSL to always use the tmp_rsa key when performing RSA
    operations.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td>
    <td>Allows initial connection to servers that do not support RI.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_MICROSOFT_SESS_ID_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_MSIE_SSLV2_RSA_PADDING</code></td>
    <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
    protocol-version vulnerability in the SSL 2.0 server implementation.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NETSCAPE_CA_DN_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_NETSCAPE_CHALLENGE_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_COMPRESSION</code></td>
    <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_QUERY_MTU</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td>
    <td>Instructs OpenSSL to always start a new session when performing
    renegotiation.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_SSLv2</code></td>
    <td>Instructs OpenSSL to turn off SSL v2</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_SSLv3</code></td>
    <td>Instructs OpenSSL to turn off SSL v3</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_TICKET</code></td>
    <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_TLSv1</code></td>
    <td>Instructs OpenSSL to turn off TLS v1</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_TLSv1_1</code></td>
    <td>Instructs OpenSSL to turn off TLS v1.1</td>
  </tr>
  <tr>
    <td><code>SSL_OP_NO_TLSv1_2</code></td>
    <td>Instructs OpenSSL to turn off TLS v1.2</td>
  </tr>
    <tr><td><code>SSL_OP_PKCS1_CHECK_1</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_PKCS1_CHECK_2</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_SINGLE_DH_USE</code></td>
    <td>Instructs OpenSSL to always create a new key when using
    temporary/ephemeral DH parameters.</td>
  </tr>
  <tr>
    <td><code>SSL_OP_SINGLE_ECDH_USE</code></td>
    <td>Instructs OpenSSL to always create a new key when using
    temporary/ephemeral ECDH parameters.</td>
  </tr>
    <tr><td><code>SSL_OP_SSLEAY_080_CLIENT_DH_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_TLS_BLOCK_PADDING_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_TLS_D5_BUG</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td>
    <td>Instructs OpenSSL to disable version rollback attack detection.</td>
  </tr>
</tbody></table>


<h3>OpenSSL Engine Constants<span><a class="mark" href="#crypto_openssl_engine_constants" id="crypto_openssl_engine_constants">#</a></span></h3>
<table>
  <tbody><tr>
    <th>Constant</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_RSA</code></td>
    <td>Limit engine usage to RSA</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_DSA</code></td>
    <td>Limit engine usage to DSA</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_DH</code></td>
    <td>Limit engine usage to DH</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_RAND</code></td>
    <td>Limit engine usage to RAND</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_ECDH</code></td>
    <td>Limit engine usage to ECDH</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_ECDSA</code></td>
    <td>Limit engine usage to ECDSA</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_CIPHERS</code></td>
    <td>Limit engine usage to CIPHERS</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_DIGESTS</code></td>
    <td>Limit engine usage to DIGESTS</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_STORE</code></td>
    <td>Limit engine usage to STORE</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_PKEY_METHS</code></td>
    <td>Limit engine usage to PKEY_METHDS</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td>
    <td>Limit engine usage to PKEY_ASN1_METHS</td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_ALL</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>ENGINE_METHOD_NONE</code></td>
    <td></td>
  </tr>
</tbody></table>


<h3>Other OpenSSL Constants<span><a class="mark" href="#crypto_other_openssl_constants" id="crypto_other_openssl_constants">#</a></span></h3>
<table>
  <tbody><tr>
    <th>Constant</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>DH_CHECK_P_NOT_PRIME</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>DH_NOT_SUITABLE_GENERATOR</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>NPN_ENABLED</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>ALPN_ENABLED</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_PKCS1_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_SSLV23_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_NO_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_PKCS1_OAEP_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_X931_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_PKCS1_PSS_PADDING</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>RSA_PSS_SALTLEN_DIGEST</code></td>
    <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the digest size
        when signing or verifying.</td>
  </tr>
  <tr>
    <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td>
    <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the maximum
        permissible value when signing data.</td>
  </tr>
  <tr>
    <td><code>RSA_PSS_SALTLEN_AUTO</code></td>
    <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be determined
        automatically when verifying a signature.</td>
  </tr>
  <tr>
    <td><code>POINT_CONVERSION_COMPRESSED</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td>
    <td></td>
  </tr>
  <tr>
    <td><code>POINT_CONVERSION_HYBRID</code></td>
    <td></td>
  </tr>
</tbody></table>


<h3>Node.js Crypto Constants<span><a class="mark" href="#crypto_node_js_crypto_constants" id="crypto_node_js_crypto_constants">#</a></span></h3>
<table>
  <tbody><tr>
    <th>Constant</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><code>defaultCoreCipherList</code></td>
    <td>Specifies the built-in default cipher list used by Node.js.</td>
  </tr>
  <tr>
    <td><code>defaultCipherList</code></td>
    <td>Specifies the active default cipher list used by the current Node.js
    process.</td>
  </tr>
</tbody></table>



      </div>
    </div>

    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js 中文文档 | Node.js 中文网
        </a>
      </div>
      
        <!-- [start-include:_toc.md] -->
<ul>
<li><a href="documentation.html">关于本文档</a></li>
<li><a href="synopsis.html">用法与例子</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a href="assert.html">断言测试</a></li>
<li><a href="async_hooks.html">异步钩子（Async Hooks）</a></li>
<li><a href="buffer.html">缓存（Buffer）</a></li>
<li><a href="addons.html">C++ 插件</a></li>
<li><a href="n-api.html">C/C++ 插件 - N-API</a></li>
<li><a href="child_process.html">子进程</a></li>
<li><a href="cluster.html">集群（Cluster）</a></li>
<li><a href="cli.html">命令行参数</a></li>
<li><a href="console.html">控制台（Console）</a></li>
<li><a href="crypto.html">加密（Crypto）</a></li>
<li><a href="debugger.html">调试器</a></li>
<li><a href="deprecations.html">废弃的 API</a></li>
<li><a href="dns.html">DNS</a></li>
<li><a href="domain.html">域（Domain）</a></li>
<li><a href="esm.html">ECMAScript 模块</a></li>
<li><a href="errors.html">错误（Errors）</a></li>
<li><a href="events.html">事件（Events）</a></li>
<li><a href="fs.html">文件系统</a></li>
<li><a href="globals.html">全局对象（Globals）</a></li>
<li><a href="http.html">HTTP</a></li>
<li><a href="http2.html">HTTP/2</a></li>
<li><a href="https.html">HTTPS</a></li>
<li><a href="inspector.html">检查工具（Inspector）</a></li>
<li><a href="intl.html">国际化</a></li>
<li><a href="modules.html">模块（Modules）</a></li>
<li><a href="net.html">网络（Net）</a></li>
<li><a href="os.html">操作系统（OS）</a></li>
<li><a href="path.html">路径（Path）</a></li>
<li><a href="perf_hooks.html">性能钩子（Performance Hooks）</a></li>
<li><a href="process.html">进程</a></li>
<li><a href="punycode.html">Punycode</a></li>
<li><a href="querystring.html">查询字符串</a></li>
<li><a href="readline.html">逐行读取</a></li>
<li><a href="repl.html">交互式解释器（REPL）</a></li>
<li><a href="stream.html">流（Stream）</a></li>
<li><a href="string_decoder.html">字符串解码</a></li>
<li><a href="timers.html">定时器（Timers）</a></li>
<li><a href="tls.html">安全传输层（TLS/SSL）</a></li>
<li><a href="tracing.html">事件跟踪（Tracing）</a></li>
<li><a href="tty.html">TTY</a></li>
<li><a href="dgram.html">UDP / 数据报</a></li>
<li><a href="url.html">URL</a></li>
<li><a href="util.html">工具集</a></li>
<li><a href="v8.html">V8</a></li>
<li><a href="vm.html">虚拟机（VM）</a></li>
<li><a href="zlib.html">压缩（ZLIB）</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a href="https://github.com/nodejs/node">GitHub 仓库和问题跟踪</a></li>
<li><a href="https://groups.google.com/group/nodejs">邮件列表</a></li>
</ul>
<!-- [end-include:_toc.md] -->

      
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
</body>
</html>
